package com.db;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Lock;

/**
 * Created by ekr on 25.08.14.
 */
public class ThreadsDemo {
    public static void main(String[] args) {    //5
        Counter c = new Counter();
        do {
            new Thread(new CounterThread(c)).start();
            new Thread(new CounterThread(c)).start();
        } while((c.getCounter() % 2) == 0);

        System.out.println(c.getCounter());
    }
}

class CounterThread implements Runnable {
    private Counter counter;

    public CounterThread(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        counter.increment();
    }
}

class Counter {
    private Lock lock;  //monitor
    private int count;

    public  void increment() {
        synchronized(this) {  //lock.lock();
            ++count;
        } //lock.release();
    }

    public synchronized int getCounter() {
        return count;
    }
}
